#include "peakgroup.h"
#include "globalconfig.h"
#include "dm/dmdef.h"

PeakGroup::PeakGroup(QWidget *parent)
    : QObject{parent}
{
    // setFixedHeight(420);
    // auto layout = new QHBoxLayout(this);
    // layout->setContentsMargins(0, 0, 0, 0);
    // layout->setSpacing(10 * ratio());

    const auto &cfgName = GlobalConfig::getInstance().cfgName();
    const auto &cfgDev = GlobalConfig::getInstance().cfgDev();
    const auto &group = GlobalConfig::getInstance().paramGroup();

    const auto input = getList(cfgDev.mus.tru_ch, tr(LABEL_INPUT_GROUP));
    const auto output = getList(cfgDev.out.tru_ch, tr(LABEL_OUTPUT_GROUP));
    for (int i = 0, size = cfgDev.mus.tru_ch + cfgDev.out.tru_ch; i < size; ++i) {
        PeakWidget *peak = nullptr;
        if (i < cfgDev.mus.tru_ch) {
            peak = new PeakWidget(i + MUS_CH1, cfgDev.out.max_gain, input, cfgName.mus[i], parent);
            peak->set(group.MusCh[i].gain, group.MusCh[i].mute);
        }
        else {
            const int index = i - cfgDev.mus.tru_ch;
            peak = new PeakWidget(index + OUT_CH1, cfgDev.out.max_gain, output, cfgName.out[index], parent);
            peak->set(group.OutCh[index].gain, group.OutCh[index].mute);
        }
        // layout->addWidget(peak, 2);
        connect(peak, &PeakWidget::speakerChanged, &GlobalConfig::getInstance(), &GlobalConfig::mute);
        connect(peak, &PeakWidget::gainChanged, &GlobalConfig::getInstance(), &GlobalConfig::gain);
        connect(peak, &PeakWidget::group, &GlobalConfig::getInstance(), &GlobalConfig::group);
        m_peaks.append(peak);
    }
    // layout->insertStretch(cfgDev.mus.tru_ch, 1);

    connect(&GlobalConfig::getInstance(), &GlobalConfig::peak, this, [=](const stCfgPeak &peak) {
        for (int i = 0; i < cfgDev.mus.tru_ch + cfgDev.out.tru_ch; ++i) {
            if (i < cfgDev.mus.tru_ch) {
                m_peaks.at(i)->peak(peak.mus[i]);
            }
            else {
                m_peaks.at(i)->peak(peak.out[i - cfgDev.mus.tru_ch]);
            }
        }
    });

    connect(&GlobalConfig::getInstance(), &GlobalConfig::refresh, this, &PeakGroup::refresh);
    connect(&GlobalConfig::getInstance(), &GlobalConfig::inputNameUpdated, this, &PeakGroup::inputNameUpdated);
    connect(&GlobalConfig::getInstance(), &GlobalConfig::outputNameUpdated, this, &PeakGroup::outputNameUpdated);
}

QVector<PeakWidget *> PeakGroup::peaks() const
{
    return m_peaks;
}

void PeakGroup::refresh()
{
    const auto group = GlobalConfig::getInstance().paramGroup();
    for (int i = 0; i < m_peaks.count(); ++i) {
        auto level = m_peaks.at(i);
        if (!level) {
            continue;
        }
        if (level->channel() <= MUS_CH4) {
            level->set(group.MusCh[level->channel() - MUS_CH1].gain, group.MusCh[level->channel() - MUS_CH1].mute);
        }
        else if (level->channel() >= OUT_CH1 && level->channel() <= OUT_CH8) {
            level->set(group.OutCh[level->channel() - OUT_CH1].gain, group.OutCh[level->channel() - OUT_CH1].mute);
        }
    }
}

void PeakGroup::inputNameUpdated()
{
    const auto input = inputNames();
    for (int i = 0; i < input.size(); ++i) {
        m_peaks.at(i)->setTitle(input.at(i));
    }
}

void PeakGroup::outputNameUpdated()
{
    const auto &cfgDev = GlobalConfig::getInstance().cfgDev();
    const auto output = outputNames();
    for (int i = 0; i < output.size(); ++i) {
        m_peaks.at(i + cfgDev.mus.tru_ch)->setTitle(output.at(i));
    }
}

QStringList PeakGroup::getList(const int &count, const QString &text) const
{
    QStringList list;
    for (int i = 0; i < count; ++i) {
        list.append(text + QString::number(i + 1));
    }
    return list;
}

QStringList PeakGroup::inputNames() const
{
    const auto &cfgName = GlobalConfig::getInstance().cfgName();
    const auto &cfgDev = GlobalConfig::getInstance().cfgDev();

    QStringList list;
    for (int i = 0; i < cfgDev.mus.tru_ch; ++i) {
        list.append(cfgName.mus[i]);
    }
    return list;
}

QStringList PeakGroup::outputNames() const
{
    const auto &cfgName = GlobalConfig::getInstance().cfgName();
    const auto &cfgDev = GlobalConfig::getInstance().cfgDev();

    QStringList list;
    for (int i = 0; i < cfgDev.out.tru_ch; ++i) {
        list.append(cfgName.out[i]);
    }
    return list;
}

